package com.yzt.zhuju.dialog;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.yzt.zhuju.R;
import com.yzt.zhuju.widget.tuiview.TUITextView;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * author : bob
 * date   : 2022/11/15
 * desc   : 底部弹窗的列表选项
 */
public class MenuSelectBaseDialog extends Dialog implements View.OnClickListener {

    private TUITextView mBtnCancel;

    OnItemClickListener mListener;

    private RecyclerView recyclerView;

    private MenuSelectBaseAdapter adapter;

    private List<String> list;

    public MenuSelectBaseDialog(Context context, int theme, List<String> list) {
        super(context, theme);
        this.list = list;
        this.initDialog(list);
    }

    public void initDialog(List<String> list) {
        this.list = list;
        Window win = this.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        win.setGravity(8388688);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = -1;
        lp.height = -2;
        win.setAttributes(lp);
        this.setCanceledOnTouchOutside(false);
        this.show();
    }

    private void hideDialog() {
        this.cancel();
        this.dismiss();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_select_menu_base);
        this.initView();
        this.initListener();
    }

    private void initView() {
        this.mBtnCancel = this.findViewById(R.id.btn_cancel);
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new MenuSelectBaseAdapter();
        recyclerView.setAdapter(adapter);
        if (list != null) {
            adapter.setNewData(list);
        }
        adapter.setOnItemClickListener(new com.chad.library.adapter.base.listener.OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                hideDialog();
                mListener.onItemClick(position);
            }
        });
    }

    private void initListener() {


        this.mBtnCancel.setOnClickListener(this);
        this.setOnKeyListener((dialog, keyCode, event) -> {
            if (keyCode == 4 && event.getAction() == 1) {
                MenuSelectBaseDialog.this.hideDialog();
                MenuSelectBaseDialog.this.mListener.onItemClick(0);
            }
            return false;
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.isOutOfBounds(this.getContext(), event)) {
            this.hideDialog();
            this.mListener.onItemClick(0);
        }
        return super.onTouchEvent(event);
    }

    private boolean isOutOfBounds(Context context, MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        View decorView = this.getWindow().getDecorView();
        return x < -slop || y < -slop || x > decorView.getWidth() + slop || y > decorView.getHeight() + slop;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_cancel) {
            this.hideDialog();
            this.mListener.onItemClick(-1);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int var1);
    }

    class MenuSelectBaseAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public MenuSelectBaseAdapter() {
            super(R.layout.item_menu_select_base);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, String item) {
            holder.setText(R.id.tv_content, item);
        }
    }
}
